package org.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@EnableAutoConfiguration
@RequestMapping(path = "/steam", produces = "application/json;charset=UTF-8")
public class SteamGamesController {
    @Autowired
    private JdbcTemplate template;

    /**
     * 平台游戏数量分布
     */
    @RequestMapping(path = "/platformCnt", method = RequestMethod.GET)
    public EchartData platformCnt() throws IOException {
        return template.query("select * from project.ads_steam_games_platform_cnt order by cnt desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }


    /**
     * 每年的新游戏
     */
    @RequestMapping(path = "/yearCnt", method = RequestMethod.GET)
    public EchartData yearCnt() throws IOException {
        return template.query("select * from project.ads_steam_games_year_cnt order by cnt desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }


    /**
     * 游戏特征词云图
     */
    @RequestMapping(path = "/tagsCnt", method = RequestMethod.GET)
    public EchartData tagsCnt() throws IOException {
        return template.query("select * from ads_steam_games_tags_cnt order by cnt desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }

    /**
     * 前20的开发商
     */
    @RequestMapping(path = "/developerCnt", method = RequestMethod.GET)
    public EchartData developerCnt() throws IOException {
        return template.query("    select * from project.ads_steam_games_developer_cnt order by cnt desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }

    /**
     * 前20的出版商
     */
    @RequestMapping(path = "/publisherCnt", method = RequestMethod.GET)
    public EchartData publisherCnt() throws IOException {
        return template.query("    select * from project.ads_steam_games_publisher_cnt order by cnt desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }

    /**
     * 游戏价格分布
     */
    @RequestMapping(path = "/priceCnt", method = RequestMethod.GET)
    public EchartData priceCnt() throws IOException {
        return template.query("select * from project.ads_steam_games_price_cnt order by cnt desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }

    /**
     * 每种游戏的好评率、差评率
     */
    @RequestMapping(path = "/categoryCnt", method = RequestMethod.GET)
    public EchartData categoryCnt() throws IOException {
        return template.query("select * from project.ads_steam_games_category_cnt order by categories desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getBigDecimal(2));
                data.zdata.add(rs.getBigDecimal(3));
            }
            return data;
        });
    }

    /**
     * 游戏年龄要求情况
     */
    @RequestMapping(path = "/ageCnt", method = RequestMethod.GET)
    public EchartData ageCnt() throws IOException {
        return template.query("select * from project.ads_steam_games_age_cnt order by cnt desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }

    public static class EchartData {

        public List<Object> xdata;
        public List<Object> ydata;
        public List<Object> zdata;
        public List<Object> wdata;
        public List<Object> vdata;

        public List<Object> hdata;

        public EchartData() {
            this.xdata = new ArrayList<Object>();
            this.ydata = new ArrayList<Object>();
            this.zdata = new ArrayList<Object>();
            this.wdata = new ArrayList<Object>();
            this.vdata = new ArrayList<Object>();
            this.hdata = new ArrayList<Object>();
        }


    }
}
